package com.lebang.controller;

import com.lebang.common.Login;
import com.lebang.entity.LbbAuthority;
import com.lebang.entity.sys.LbbRole;
import com.lebang.mapper.LbbAuthorityMapper;
import com.lebang.model.enums.Booleans;
import com.lebang.model.vo.RoleVo;
import com.lebang.model.vo.SearchVo;
import com.lebang.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 * @author weizx
 * @className UserController
 * @date 2018年7月9日 下午4:45:09
 */
@Controller
public class RoleController {


    @Autowired
    private RoleService roleService;
    @Autowired
    private LbbAuthorityMapper lbbAuthorityMapper;

    /**
     * 角色列表
     *
     * @param model
     * @param session
     * @return
     * @author weizx
     * @date 2018年7月11日 下午5:44:31
     */
    @Login
    @RequestMapping(value = {"/role/list"}, method = {RequestMethod.GET})
    public String list() {
        return "/role/list";
    }


    /**
     * 分组列表
     *
     * @param vo
     * @return
     * @author weizx
     * @date 2018年7月11日 下午5:45:04
     */
    @RequestMapping(value = {"/role/list"}, method = {RequestMethod.POST})
    @ResponseBody
    @Login
    public Map<String, Object> list(SearchVo vo) {
        return roleService.list(vo);
    }

    /**
     * 新增
     *
     * @return
     * @author weizx
     * @date 2018年7月11日 下午8:45:58
     */
    @Login
    @RequestMapping(value = {"/role/insert"}, method = {RequestMethod.GET})
    public String insert() {
        return "/role/edit";
    }

    /**
     * 编辑
     *
     * @param model
     * @return
     * @author weizx
     * @date 2018年7月11日 下午8:45:58
     */
    @Login
    @RequestMapping(value = {"/role/edit/{roleid}"}, method = {RequestMethod.GET})
    public String edit(@PathVariable String roleid, Model model) {
        LbbRole role = roleService.getByRoleId(roleid);
        model.addAttribute("role", role);
        return "/role/edit";
    }

    /**
     * 角色权限
     *
     * @param roleid
     * @param model
     * @return
     * @author weizx
     * @date 2018年7月12日 下午12:01:48
     */
    @Login
    @RequestMapping(value = {"/role/authority/{roleid}"}, method = {RequestMethod.GET})
    public String role(@PathVariable String roleid, Model model) {
        List<Map<String, Object>> authorities = roleService.getAuthorities(roleid);
        model.addAttribute("authorities", authorities);
        model.addAttribute("roleid", roleid);
        return "/role/authority";
    }

    @Login
    @RequestMapping(value = {"/role/save"}, method = {RequestMethod.POST})
    @ResponseBody
    public String save(RoleVo vo) {
        if (vo.getRoleId() == null || "".equals(vo.getRoleId())) {
            roleService.insert(vo);
        } else {
            if (vo.getRoleId().equals(vo.getParentId())) {
                return "不能基于自己创建！";
            }
            if (null != vo.getParentId() && !"".equals(vo.getParentId())) {
                if (roleService.countByParentId(vo.getParentId()) > 0) {
                    return "不能基于多级角色创建！";
                }
            }
            roleService.update(vo);
        }
        return "保存成功！";
    }

    /**
     * 删除
     *
     * @param roleid
     * @return
     * @author weizx
     * @date 2018年7月12日 下午3:45:03
     */
    @Login
    @RequestMapping(value = {"/role/del/{roleid}"}, method = {RequestMethod.POST})
    @ResponseBody
    public String del(@PathVariable String roleid) {
        LbbRole role = roleService.getByRoleId(roleid);
        if (role == null) {
            return "角色不存在";
        }

        if (roleService.countByParentId(roleid) > 0) {
            return "请先移除上层分组！";
        }
        roleService.delete(role);
        return "删除成功！";
    }

    /**
     * 角色权限
     *
     * @param roleid
     * @param roleid
     * @return
     * @author weizx
     * @date 2018年7月12日 下午4:50:24
     */
    @Login
    @RequestMapping(value = {"/role/authority"}, method = {RequestMethod.POST})
    @ResponseBody
    public String authority(@RequestParam("roleid") String roleid, @RequestParam("resource") String resource) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("role_id", roleid);
        params.put("resource", resource);
        LbbAuthority auth = lbbAuthorityMapper.selectByroleIdAndresource(roleid, resource);
        if (auth == null) {
            auth = new LbbAuthority();
            auth.setResource(resource);
            auth.setRoleId(roleid);
            auth.setHasDeleted(Booleans.False.name());
            auth.setCreateTime(new Date());
            lbbAuthorityMapper.insertSelective(auth);
        } else {
            auth.setHasDeleted("True");
            lbbAuthorityMapper.updateByPrimaryKeySelective(auth);
        }
        return "OK";
    }

}
